package com.capgemini.scaffold.controller;

import com.capgemini.scaffold.common.Admin;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.model.Dict;
import com.capgemini.scaffold.model.request.DictAddReq;
import com.capgemini.scaffold.model.request.DictQueryReq;
import com.capgemini.scaffold.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.capgemini.scaffold.model.User;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import static com.capgemini.scaffold.constant.UserConstant.USER_LOGIN_STATE;
import static com.capgemini.scaffold.constant.UserConstant.ADMIN_ROLE;
/**
 * 字典接口
 *
 * @Author: llf
 */

@Api(tags = "字典管理")
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    @ApiOperation("获取职位列表")
    @PostMapping("/getPosition")
    public ResponseBean<List<Dict>> GetDictPositon (){
        return ResponseBean.success(dictService.getPosition());
    }

    @ApiOperation("获取部门列表")
    @PostMapping("/getDepartment")
    public ResponseBean<List<Dict>> GetDictDepartment(){
        return ResponseBean.success(dictService.getDepartment());
    }

    @ApiOperation("获取职级列表")
    @PostMapping("/getLevel")
    public ResponseBean<List<Dict>> GetDictLevel(){
        return ResponseBean.success(dictService.getLevel());
    }

    @ApiOperation("根据请求体查询")
    @PostMapping("/queryDict")
    public ResponseBean<List<Dict>> QueryDict(@RequestBody DictQueryReq dictReq){
       return ResponseBean.success(dictService.queryDict(dictReq));
    }

//    @ApiOperation("空参查询")
//    @PostMapping("/queryAll")
//    public ResponseBean<List<Dict>> QueryAll(){
//        return ResponseBean.success(dictService.queryAll());
//    }

    @ApiOperation("添加字典")
    @PostMapping("/addDict")
    public ResponseBean<Dict> AddDict(@RequestBody DictAddReq dictAddReq){
        return ResponseBean.success(dictService.addDict(dictAddReq));
    }

    @ApiOperation("更新字典")
    @PostMapping("/updateDict")
    public ResponseBean<Dict> UpdateDict(@RequestBody DictAddReq dictAddReq){
        return ResponseBean.success(dictService.updateDict(dictAddReq));
    }

    @ApiOperation("获取去重后的dict类型列表")
    @PostMapping("/getDictGroup")
    public ResponseBean<List<String>> GetDictGroup(){
        return ResponseBean.success(dictService.getDictGroup());
    }

    @ApiOperation("根据id删除字典")
    @DeleteMapping("/deleteDict/{codeId}/{isRole}")
    public ResponseBean<Integer> DeleteDict(@PathVariable Integer codeId,@PathVariable Integer isRole){
        if(!Admin.isAdmin(isRole)){
            throw new BusinessException(ExceptionEnum.NO_AUTH,"你没有权限");
        }
        if(codeId <= 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"该字典不存在");
        }
        boolean b = dictService.deleteDict(codeId);
        return ResponseBean.success(b);
    }

    @ApiOperation("字典翻译")
    @PostMapping("/translateDict")
    public ResponseBean<String> TranslateDict(String dictId){
        return ResponseBean.success(dictService.translateDict(dictId));
    }
}
